<template>
  <el-card>
    <el-form ref="form" :inline="true" :model="attrForm">
      <el-form-item label="一级分类">
        <el-select
          clearable
          v-model="attrForm.c1Id"
          placeholder="请选择一级分类"
          @change="getC2"
        >
          <draggable
            v-model="c1Arr"
            group="people"
            @start="drag = true"
            @end="drag = false"
          >
            <el-option
              :key="c1.id"
              v-for="c1 in c1Arr"
              :label="c1.name"
              :value="c1.id"
            ></el-option>
          </draggable>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          @change="getC3"
          v-model="attrForm.c2Id"
          placeholder="请选择二级分类"
          clearable
        >
          <el-option
            :key="c2.id"
            v-for="c2 in c2Arr"
            :label="c2.name"
            :value="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          clearable
          v-model="attrForm.c3Id"
          placeholder="请选择三级分类"
          clearable
          @change="c3Handle"
        >
          <el-option
            :key="c3.id"
            v-for="c3 in c3Arr"
            :label="c3.name"
            :value="c3.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import draggable from "vuedraggable";
export default {
  name: "Category",
  components: {
    draggable,
  },
  props: ["fetchCategoryId"],
  data() {
    return {
      attrForm: {
        c1Id: "",
        c2Id: "",
        c3Id: "",
      },
      c1Arr: [],
      c2Arr: [],
      c3Arr: [],
    };
  },
  methods: {
    async getC1() {
      let res = await this.$http.attr.reqC1();
      if (res.code == 200) this.c1Arr = res.data;
    },
    //一级分类change事件触发时的回调
    async getC2() {
      let { c1Id } = this.attrForm;
      this.fetchCategoryId(c1Id, 1);
      this.attrForm.c2Id = "";
      this.attrForm.c3Id = "";
      this.c2Arr= [];
      this.c3Arr= [];
      let res = await this.$http.attr.reqC2(c1Id);
      if (res.code == 200) this.c2Arr = res.data;
    },
    
    //二级分类change事件触发时的回调
    async getC3() {
      this.attrForm.c3Id = "";
      this.c3Arr= [];
      let { c2Id } = this.attrForm;
      this.fetchCategoryId(c2Id, 2);
      let res = await this.$http.attr.reqC3(c2Id);
      if (res.code == 200) this.c3Arr = res.data;
    },

    //三级分类change事件触发时的回调
    c3Handle() {
      let { c3Id } = this.attrForm;
      this.fetchCategoryId(c3Id, 3);
    },
  },
  mounted() {
    this.getC1();
  },
};
</script>

<style>
</style>